//
//  JMMeWarningPopupView.m
//  NewiOSProject
//
//  Created by li on 2023/9/26.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMMeWarningPopupView.h"
#import "LayoutView.h"

#import "JMPickerSelectionView.h"
#import "JMPickerSelectionModel.h"
#import "PaddingView.h"
#import "LayoutScrollView.h"
#import "CustomStackView.h"
#import "UIView+PopupProtocol.h"

@interface JMMeWarningPopupView ()
@property(nonatomic, strong) CAGradientLayer *gl;
@end

@implementation JMMeWarningPopupView
@synthesize modalStyle;
@synthesize translucentView;
@synthesize dismissCompletion;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.translucentView = [self defaultTranslucentView];
        
        self.modalStyle = PopupViewModalStyleCenterScale;
        [self configureLayer];
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    CustomStackView *rootStack = [CustomStackView init:(UILayoutConstraintAxisVertical)];
    
    rootStack.translatesAutoresizingMaskIntoConstraints = NO;
    rootStack.spacing = 20;
    [self addSubview:rootStack];
    [NSLayoutConstraint activateConstraints:@[
        [rootStack.topAnchor constraintEqualToAnchor:self.topAnchor constant:0],
        [rootStack.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:0],
        [rootStack.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0],
        [rootStack.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:0],
    ]];
    
    CustomStackView *contentStack = [CustomStackView init:(UILayoutConstraintAxisVertical)];
    contentStack.alignment = UIStackViewAlignmentCenter;
    
    UIImageView *imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"me_fico_close"];
    [contentStack addArrangedSubview:imageView];
    [contentStack setCustomSpacing:16 afterView:imageView];
    
    UILabel *contentLab = [UILabel new];
    contentLab.numberOfLines = 0;
    contentLab.textAlignment = NSTextAlignmentCenter;
    contentLab.text = @"FICO Score ha sido pagado, no se ha generado ningun informe, por favor cambie su informacion personal para volver a aprobar";
    contentLab.textColor = [UIColor colorWithHexString:@"#0F1511"];
    contentLab.font = [UIFont systemFontOfSize:14 weight:(UIFontWeightMedium)];
    [contentStack addArrangedSubview:contentLab];
    [contentStack setCustomSpacing:32 afterView:contentLab];
    
    UIButton *action = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.confirm = action;
    action.backgroundColor = [UIColor colorWithHexString:@"#0F1511"];
    [action setTitle:@"Ir a modificar" forState:(UIControlStateNormal)];
    [action setTitleColor:[UIColor colorWithHexString:@"#FEFFCC"] forState:(UIControlStateNormal)];
    action.titleLabel.font = [UIFont systemFontOfSize:16 weight:(UIFontWeightMedium)];
    action.translatesAutoresizingMaskIntoConstraints = NO;
    [contentStack addArrangedSubview:action];
    action.layer.cornerRadius = 12;
    action.layer.masksToBounds = YES;
    [NSLayoutConstraint activateConstraints:@[
        [action.widthAnchor constraintEqualToAnchor:contentStack.widthAnchor],
        [action.heightAnchor constraintEqualToConstant:46]
    ]];

    
    [rootStack addArrangedSubview:[[PaddingView initWithContentView:contentStack edgeInsets:(UIEdgeInsetsMake(20, 20, 30, 20))] then:^(PaddingView * _Nonnull view) {
        
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 30;
        view.layer.masksToBounds = YES;
        [view.layer insertSublayer:self.gl atIndex:0];
    }]];
    
    
    UIButton *close = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [close setImage:[UIImage imageNamed:@"close_white"] forState:(UIControlStateNormal)];
    close.frame = CGRectMake(0, 0, 44, 44);
    [close addTarget:self action:@selector(closeAtion:) forControlEvents:(UIControlEventTouchUpInside)];
//    close.userInteractionEnabled = NO;
//    UIImageView *close = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"close_white"]];
    [rootStack addArrangedSubview:close];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.gl.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

- (void)configureLayer {
    CAGradientLayer *gl = [CAGradientLayer layer];
    self.gl = gl;
    gl.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    gl.startPoint = CGPointMake(0.5, 0.04);
    gl.endPoint = CGPointMake(0.5, 0.37);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:251/255.0 green:255/255.0 blue:211/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
}

- (void)closeAtion: (UIButton *)sender {
    [self dismissPopup];
}


@end
